<template>
	<view class="comNav">
		<!-- <scroll-view scroll-x class="nav text-left bg-white text-lg text-bold">
			<view class="cu-item" :class="index==TabCur?'text-orange cur':''" v-for="(item,index) in list" :key="index" @tap="tabSelect" :data-id="index">
				{{item}}
			</view>
		</scroll-view> -->
		<scroll-view scroll-x class="bg-white nav">
			<view class="flex text-center">
				<view class="cu-item flex-sub" :class="index==TabCur?'text-orange cur':''" v-for="(item,index) in list"
					:key="index" @tap="tabSelect" :data-id="index">
					{{item[defaultProps.label]}}
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		props: {
			list: {
				type: Array
			},
			value: {
				type: [String, Number],
				default: 0
			},
			defaultProps: {
				type: [Object, String],
				default: () => {
					return {
						label: "label",
						value: "value"
					}
				}
			}
		},
		data() {
			return {
				TabCur: 0,
				scrollLeft: 0
			}
		},
		watch: {
			list() {
				console.log("listChange")
				this.init()
			},
		},
		mounted() {
			if (this.list) {
				this.init()
			}
		},
		methods: {
			init() {
				console.log(this.value)
				if (this.value) {
					this.TabCur = this.list.findIndex(item => item[this.defaultProps.value] == this.value)
				}
				console.log("this.TabCur", this.TabCur)
			},
			tabSelect(e) {
				this.TabCur = e?.currentTarget.dataset.id || 0;
				console.log("this.TabCur", this.TabCur)
				this.scrollLeft = (e?.currentTarget.dataset.id - 1) * 60
				this.$emit("input", this.list[this.TabCur][this.defaultProps.value])
				this.$emit("change")
			}
		}
	}
</script>

<style lang="scss">
	.comNav {
		.cu-item {
			margin: 0 50rpx;
		}
	}
</style>